from PyQt5.QtCore import Qt, pyqtSlot,QSystemSemaphore, QSharedMemory, QCoreApplication
from PyQt5.QtWidgets import QApplication, QMessageBox, QDialog, QWidget, QMainWindow, QGridLayout, QPushButton, QVBoxLayout, QHBoxLayout
import sys
import subprocess
import os
from smu3003 import SMU3003


class SwitchBoxDialog(QDialog):
    def __init__(self, parent=None):
        """初始化"""
        # 首先把"父类"的能力获取过来
        super().__init__(parent)
        # 标题
        title = '滤波器控制单元'
        self.setWindowTitle(title)
        # 开关箱
        ip = "192.168.1.218"
        self.switch_box = SMU3003(ip)
        pass


    def push_button_LTE1_clicked(self):
        self.switch_box.switch_to_LTE1()

    def push_button_LTE2_clicked(self):
        self.switch_box.switch_to_LTE2()

    def push_button_LTE3_clicked(self):
        self.switch_box.switch_to_LTE3()

    def push_button_LTE4_clicked(self):
        self.switch_box.switch_to_LTE4()

    def push_button_NB1_clicked(self):
        self.switch_box.switch_to_NB1()

    def push_button_NB2_clicked(self):
        self.switch_box.switch_to_NB2()

    def push_button_NB3_clicked(self):
        self.switch_box.switch_to_NB3()

    def push_button_NB4_clicked(self):
        self.switch_box.switch_to_NB4()

    def push_button_824_894_clicked(self):
        self.switch_box.switch_to_824_894()

    def push_button_880_960_clicked(self):
        self.switch_box.switch_to_880_960()

    def push_button_1710_1880_clicked(self):
        self.switch_box.switch_to_1710_1880()

    def push_button_1880_1920_clicked(self):
        self.switch_box.switch_to_1880_1920()
    def push_button_1920_2170_clicked(self):
        self.switch_box.switch_to_1920_2170()

    def push_button_2300_2400_clicked(self):
        self.switch_box.switch_to_2300_2400()
    def push_button_2580_2620_clicked(self):
        self.switch_box.switch_to_2580_2620()

    def push_button_2496_2690_clicked(self):
        self.switch_box.switch_to_2496_2690()

    def push_button_stright_clicked(self):
        self.switch_box.switch_to_stright()

    def push_button_open_clicked(self):
        self.switch_box.switch_to_open()

    def push_button_amp_clicked(self):
        self.switch_box.switch_to_amp()

    def push_button_no_amp_clicked(self):
        self.switch_box.switch_to_no_amp()

    def startbutton_clicked(self):
        self.switch_box.smu3003_init()

    def run_main2_clicked(self):
        """运行main2.py中的网络分析仪测试代码"""
        try:
            # 获取当前脚本目录
            current_dir = os.path.dirname(os.path.abspath(__file__))
            main2_path = os.path.join(current_dir, "main2.py")
            
            if os.path.exists(main2_path):
                # 使用python解释器运行main2.py
                subprocess.Popen(["python", main2_path], cwd=current_dir)
                print("已启动main2.py网络分析仪测试程序")
            else:
                print("错误：找不到main2.py文件")
                
        except Exception as e:
            print(f"运行main2.py时出错: {e}")


# 按间距中的绿色按钮以运行脚本。
if __name__ == '__main__':
    app = QApplication([])
    wind = SwitchBoxDialog()
    button_min_hight = 80
    button_min_width = 120
    wind.setMinimumHeight(600)
    wind.setMinimumWidth(1000)

    push_button_statr_connect = QPushButton("statr_connect")

    # LTE buttons
    push_button_LTE1 = QPushButton("LTE1")
    push_button_LTE1.setMinimumHeight(button_min_hight)
    push_button_LTE1.setMinimumWidth(button_min_width)
    push_button_LTE1.clicked.connect(wind.push_button_LTE1_clicked)

    push_button_LTE2 = QPushButton("LTE2")
    push_button_LTE2.setMinimumHeight(button_min_hight)
    push_button_LTE2.setMinimumWidth(button_min_width)
    push_button_LTE2.clicked.connect(wind.push_button_LTE2_clicked)

    push_button_LTE3 = QPushButton("LTE3")
    push_button_LTE3.setMinimumHeight(button_min_hight)
    push_button_LTE3.setMinimumWidth(button_min_width)
    push_button_LTE3.clicked.connect(wind.push_button_LTE3_clicked)

    push_button_LTE4 = QPushButton("LTE4")
    push_button_LTE4.setMinimumHeight(button_min_hight)
    push_button_LTE4.setMinimumWidth(button_min_width)
    push_button_LTE4.clicked.connect(wind.push_button_LTE4_clicked)

    # NB buttons
    push_button_NB1 = QPushButton("NB1")
    push_button_NB1.setMinimumHeight(button_min_hight)
    push_button_NB1.setMinimumWidth(button_min_width)
    push_button_NB1.clicked.connect(wind.push_button_NB1_clicked)

    push_button_NB2 = QPushButton("NB2")
    push_button_NB2.setMinimumHeight(button_min_hight)
    push_button_NB2.setMinimumWidth(button_min_width)
    push_button_NB2.clicked.connect(wind.push_button_NB2_clicked)

    push_button_NB3 = QPushButton("NB3")
    push_button_NB3.setMinimumHeight(button_min_hight)
    push_button_NB3.setMinimumWidth(button_min_width)
    push_button_NB3.clicked.connect(wind.push_button_NB3_clicked)

    push_button_NB4 = QPushButton("NB4")
    push_button_NB4.setMinimumHeight(button_min_hight)
    push_button_NB4.setMinimumWidth(button_min_width)
    push_button_NB4.clicked.connect(wind.push_button_NB4_clicked)

    # Frequency band buttons
    push_button_824_894 = QPushButton("824-894")
    push_button_824_894.setMinimumHeight(button_min_hight)
    push_button_824_894.setMinimumWidth(button_min_width)
    push_button_824_894.clicked.connect(wind.push_button_824_894_clicked)

    push_button_880_960 = QPushButton("880-960")
    push_button_880_960.setMinimumHeight(button_min_hight)
    push_button_880_960.setMinimumWidth(button_min_width)
    push_button_880_960.clicked.connect(wind.push_button_880_960_clicked)

    push_button_1710_1880 = QPushButton("1710-1880")
    push_button_1710_1880.setMinimumHeight(button_min_hight)
    push_button_1710_1880.setMinimumWidth(button_min_width)
    push_button_1710_1880.clicked.connect(wind.push_button_1710_1880_clicked)

    push_button_1880_1920 = QPushButton("1880-1920")
    push_button_1880_1920.setMinimumHeight(button_min_hight)
    push_button_1880_1920.setMinimumWidth(button_min_width)
    push_button_1880_1920.clicked.connect(wind.push_button_1880_1920_clicked)

    push_button_1920_2170 = QPushButton("1920-2170")
    push_button_1920_2170.setMinimumHeight(button_min_hight)
    push_button_1920_2170.setMinimumWidth(button_min_width)
    push_button_1920_2170.clicked.connect(wind.push_button_1920_2170_clicked)

    push_button_2300_2400 = QPushButton("2300-2400")
    push_button_2300_2400.setMinimumHeight(button_min_hight)
    push_button_2300_2400.setMinimumWidth(button_min_width)
    push_button_2300_2400.clicked.connect(wind.push_button_2300_2400_clicked)

    push_button_2580_2620 = QPushButton("2580-2620")
    push_button_2580_2620.setMinimumHeight(button_min_hight)
    push_button_2580_2620.setMinimumWidth(button_min_width)
    push_button_2580_2620.clicked.connect(wind.push_button_2580_2620_clicked)

    push_button_2496_2690 = QPushButton("2496-2690")
    push_button_2496_2690.setMinimumHeight(button_min_hight)
    push_button_2496_2690.setMinimumWidth(button_min_width)
    push_button_2496_2690.clicked.connect(wind.push_button_2496_2690_clicked)

    # Other buttons
    push_button_stright = QPushButton("Stright")
    push_button_stright.setMinimumHeight(button_min_hight)
    push_button_stright.setMinimumWidth(button_min_width)
    push_button_stright.clicked.connect(wind.push_button_stright_clicked)

    push_button_open = QPushButton("Open")
    push_button_open.setMinimumHeight(button_min_hight)
    push_button_open.setMinimumWidth(button_min_width)
    push_button_open.clicked.connect(wind.push_button_open_clicked)

    push_button_no_amp = QPushButton("无放大器")
    push_button_no_amp.setMinimumHeight(button_min_hight)
    push_button_no_amp.setMinimumWidth(button_min_width)
    push_button_no_amp.clicked.connect(wind.push_button_no_amp_clicked)

    push_button_amp = QPushButton("放大器")
    push_button_amp.setMinimumHeight(button_min_hight)
    push_button_amp.setMinimumWidth(button_min_width)
    push_button_amp.clicked.connect(wind.push_button_amp_clicked)

    # 添加运行main2.py的按钮
    push_button_run_main2 = QPushButton("网络分析仪测试")
    push_button_run_main2.setMinimumHeight(button_min_hight)
    push_button_run_main2.setMinimumWidth(button_min_width)
    push_button_run_main2.setStyleSheet("background-color:rgb(255, 165, 0);")
    push_button_run_main2.clicked.connect(wind.run_main2_clicked)

    grid_layout = QGridLayout()
    grid_layout.addWidget(push_button_LTE1, 0, 0, 1, 1, Qt.AlignVCenter | Qt.AlignHCenter)
    grid_layout.addWidget(push_button_LTE2, 0, 1, 1, 1, Qt.AlignVCenter | Qt.AlignHCenter)
    grid_layout.addWidget(push_button_LTE3, 0, 2, 1, 1, Qt.AlignVCenter | Qt.AlignHCenter)
    grid_layout.addWidget(push_button_LTE4, 0, 3, 1, 1, Qt.AlignVCenter | Qt.AlignHCenter)
    grid_layout.addWidget(push_button_NB1, 0, 5, 1, 1, Qt.AlignVCenter | Qt.AlignHCenter)
    grid_layout.addWidget(push_button_NB2, 0, 6, 1, 1, Qt.AlignVCenter | Qt.AlignHCenter)
    grid_layout.addWidget(push_button_NB3, 0, 7, 1, 1, Qt.AlignVCenter | Qt.AlignHCenter)
    grid_layout.addWidget(push_button_NB4, 0, 8, 1, 1, Qt.AlignVCenter | Qt.AlignHCenter)

    grid_layout.addWidget(push_button_824_894, 1, 0, 1, 1, Qt.AlignVCenter | Qt.AlignHCenter)
    grid_layout.addWidget(push_button_880_960, 1, 1, 1, 1, Qt.AlignVCenter | Qt.AlignHCenter)
    grid_layout.addWidget(push_button_1710_1880, 1, 2, 1, 1, Qt.AlignVCenter | Qt.AlignHCenter)
    grid_layout.addWidget(push_button_1880_1920, 1, 3, 1, 1, Qt.AlignVCenter | Qt.AlignHCenter)
    grid_layout.addWidget(push_button_1920_2170, 1, 4, 1, 1, Qt.AlignVCenter | Qt.AlignHCenter)
    grid_layout.addWidget(push_button_2300_2400, 1, 5, 1, 1, Qt.AlignVCenter | Qt.AlignHCenter)
    grid_layout.addWidget(push_button_2580_2620, 1, 6, 1, 1, Qt.AlignVCenter | Qt.AlignHCenter)
    grid_layout.addWidget(push_button_2496_2690, 1, 7, 1, 1, Qt.AlignVCenter | Qt.AlignHCenter)
    grid_layout.addWidget(push_button_stright, 1, 8, 1, 1, Qt.AlignVCenter | Qt.AlignHCenter)
    grid_layout.addWidget(push_button_open, 1, 9, 1, 1, Qt.AlignVCenter | Qt.AlignHCenter)

    grid_layout.addWidget(push_button_amp, 2, 0, 1, 1, Qt.AlignVCenter | Qt.AlignHCenter)
    grid_layout.addWidget(push_button_no_amp, 2, 1, 1, 1, Qt.AlignVCenter | Qt.AlignHCenter)
    grid_layout.addWidget(push_button_run_main2, 2, 2, 1, 1, Qt.AlignVCenter | Qt.AlignHCenter)

    grid_layout.setHorizontalSpacing(20)
    grid_layout.setVerticalSpacing(20)
    grid_layout.setContentsMargins(10, 10, 10, 10)
    main_layout = QVBoxLayout()
    push_button_layout = QHBoxLayout()
    push_button_layout.addWidget(push_button_statr_connect)
    push_button_statr_connect.setMaximumWidth(120)
    push_button_statr_connect.setMinimumHeight(40)
    push_button_statr_connect.setStyleSheet("background-color:rgb(0, 250, 255);")
    push_button_statr_connect.clicked.connect(wind.startbutton_clicked)
    main_layout.addLayout(push_button_layout)
    main_layout.addLayout(grid_layout)
    wind.setLayout(main_layout)
    wind.show()
    sys.exit(app.exec_())

